<template>
    <div class="user-profile">
        <img src="@/assets/user.png" alt="User Avatar" class="avatar" />
        <div class="user-info">
            <p>21004XXXXXXXX</p>
            <p>学生</p>
        </div>
    </div>

    <div class="mycourse">
        <div class="main-content">
            <p class="yjrkc">已加入课程</p>
            <div class="tabs">
                <ul>
                    <li :class="{ 'active': selectedTab === '全部' }" @click="setTab('全部')">全部</li>
                    <li :class="{ 'active': selectedTab === '正在进行' }" @click="setTab('正在进行')">正在进行</li>
                    <li :class="{ 'active': selectedTab === '已结束' }" @click="setTab('已结束')">已结束</li>
                </ul>
                <div class="search-container">
                    <input type="text" placeholder="搜索我的课程" class="search" v-model="searchQuery"
                        @keydown.enter="searchResources" />
                    <img src="@/assets/icon-search.png" alt="Search Icon" class="search-icon"
                        @click="searchResources" />
                </div>
            </div>

            <div class="course-container">
                <div class="course-card" v-for="course in paginatedCourses" :key="course.id">
                    <div class="course-placeholder"><img :src="course.resource_url" alt="课程图片" class="course-image">
                    </div>
                    <div class="course-status">{{ course.status }}</div>
                    <div class="course-info">
                        <h3>{{ course.title }}</h3>
                        <p>{{ course.date }}</p>
                    </div>
                </div>
            </div>

            <div class="pagination">
                <button class="pagination-button" @click="prevPage"
                    :class="{ 'disabled': currentPage === 1 }">上一页</button>
                <span v-for="page in totalPages" :key="page" @click="setPage(page)"
                    :class="{ 'active-page': page === currentPage, 'disabled': page === currentPage }">
                    {{ page }}
                </span>
                <button class="pagination-button" @click="nextPage"
                    :class="{ 'disabled': currentPage === totalPages }">下一页</button>
            </div>
        </div>
    </div>
</template>

<script>
export default {
    name: 'MyCourseView',
    data() {
        return {
            courses: [
                { id: 1, title: '计算机基础', date: '07月03日 19:00', status: '正在进行' },
                { id: 2, title: '计算机基础', date: '07月03日 19:00', status: '正在进行' },
                { id: 3, title: '计算机基础', date: '07月03日 19:00', status: '已结束' },
                { id: 4, title: '计算机基础', date: '07月03日 19:00', status: '已结束' },
                { id: 5, title: '计算机基础', date: '07月03日 19:00', status: '正在进行' },
                { id: 6, title: '计算机基础', date: '07月03日 19:00', status: '已结束' },
                { id: 7, title: '计算机基础', date: '07月03日 19:00', status: '正在进行' },
                { id: 8, title: '形势', date: '07月03日 19:00', status: '已结束' },
                { id: 9, title: '虚拟', date: '07月03日 19:00', status: '已结束' },
                { id: 10, title: '高数', date: '07月03日 19:00', status: '已结束' },
                { id: 11, title: '数媒', date: '07月03日 19:00', status: '已结束' },
                { id: 12, title: '虚拟', date: '07月03日 19:00', status: '已结束' },
                { id: 13, title: '高数', date: '07月03日 19:00', status: '已结束' },
                { id: 14, title: '数媒', date: '07月03日 19:00', status: '已结束' }
            ],
            currentPage: 1,
            selectedTab: '全部',
            searchQuery: ''
        }
    },
    computed: {
        filteredCourses() {
            if (this.selectedTab === '全部') {
                return this.courses.filter(course =>
                    course.title.toLowerCase().includes(this.searchQuery.toLowerCase())
                );
            } else {
                return this.courses.filter(course =>
                    course.status === this.selectedTab &&
                    course.title.toLowerCase().includes(this.searchQuery.toLowerCase())
                );
            }
        },
        // 根据当前页和每页显示数量计算出的分页后的课程列表
        paginatedCourses() {
            const pageSize = 5 // 每页显示数量
            const startIndex = (this.currentPage - 1) * pageSize;
            return this.filteredCourses.slice(startIndex, startIndex + pageSize);
        },
        // 计算总页数
        totalPages() {
            const pageSize = 5; // 每页显示数量
            return Math.ceil(this.filteredCourses.length / pageSize);
        }
    },
    methods: {
        setTab(tab) {
            this.selectedTab = tab
            this.currentPage = 1; // 切换标签时，重置当前页为第一页
        },
        setPage(page) {
            this.currentPage = page
        },
        prevPage() {
            if (this.currentPage > 1) this.currentPage--
        },
        nextPage() {
            if (this.currentPage < this.totalPages) this.currentPage++;
        },
        searchResources() {
            // This method can be extended to perform actual search functionality if needed
            // Currently, it just handles the search query input
        }
    }
}
</script>

<style scoped>
.yjrkc {
    text-align: left;
    font-weight: bold;
    margin-left: 15px;

}

.mycourse {
    display: flex;
    background-color: #f9f9f9;
    padding: 5px 150px 20px 150px;

}

.main-content {
    flex: 5;
    background-color: #ffffff;
    border-radius: 8px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}


/* 用户信息样式 */
.user-profile {
    display: flex;
    align-items: center;
    padding: 20px;
    background-size: cover;
    /* 背景图尺寸适应容器 */
    background-image: url('@/assets/mycourse-bg.png');
}

.avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
    margin-left: 150px;
}

.user-info {
    margin-left: 20px;
    text-align: left;
}

/* 选项卡样式 */
.tabs {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 20px;
    background: white;
    margin-top: 10px;
    position: relative;
    /* 添加相对定位 */
}

.tabs::after {
    content: '';
    position: absolute;
    top: 60px;
    left: 0;
    bottom: 0;
    width: 95%;
    margin-left: 30px;
    margin-right: 30px;
    height: 1px;
    /* 控制横线的粗细 */
    background-color: #ccc;
    /* 灰色横线 */
}

.tabs ul {
    display: flex;
    list-style: none;
    gap: 20px;
}

.tabs ul li {
    cursor: pointer;
    padding: 5px 10px;
}

.tabs ul .active {
    border-bottom: 2px solid rgb(59, 150, 255);
    font-weight: bold;
}

.search-container {
    display: flex;
    align-items: center;
    margin-left: auto;
    margin-right: 30px;
    padding: 1px 10px;
    margin-bottom: 5px;
    border: 1px solid #2a82e4;
    border-radius: 25px;
    background-color: #fff;
}

.search {
    border: none;
    outline: none;
    padding: 5px;
    border-radius: 25px;
    flex: 1;
    margin-right: 10px;
}

.search-icon {
    width: 30px;
    height: 30px;
    cursor: pointer;
}

/* 课程容器样式 */
.course-container {
    display: flex;
    flex-wrap: wrap;
    /* justify-content: space-between; */
    padding: 20px;
    background: white;
    /* margin-top: 10px; */
}

.course-card {
    border: 1px solid #ddd;
    border-radius: 8px;
    /* padding: 20px; */
    margin: 10px;
    background: #fff;
    width: calc(20% - 22px);
    box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.course-placeholder {
    height: 120px;
    background-color: rgba(0, 0, 0, 0.1);
    /* 黑色阴影块 */
    margin-bottom: 1rem;
    border-radius: 8px 8px 0 0;
    overflow: hidden;
    /* 防止图片超出div部分 */
}

.course-image {
    background: #ccc;
    width: 100%;
    height: 120px;
    border-radius: 8px 8px 0 0;
    margin-bottom: 10px;
}

.course-status {
    background: rgb(59, 150, 255);
    color: white;
    width: 90px;
    padding: 5px 5px;
    border-radius: 0 13px 13px 0;
    text-align: center;
    /* margin-bottom: 10px; */
}

.course-info {
    display: flex;
    flex-direction: column;
    text-align: left;
    margin-left: 10px;
    /* gap: 5px; */
}

.course-info p {
    margin-top: -6px;
}

/* 分页样式 */
.pagination {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 10px;
    padding: 20px;
}

.pagination-button,
.pagination span {
    padding: 5px 10px;
    color: black;
    border: 2px solid rgb(59, 150, 255);
    background-color: white;
    cursor: pointer;
    transition: background-color 0.3s, color 0.3s;
    border-radius: 4px;
}

.pagination-button:hover,
.pagination span:hover {
    background-color: rgb(59, 150, 255);
    color: white;
}

.pagination span.active-page {
    font-weight: bold;
    background-color: rgb(59, 150, 255);
    color: white;
    cursor: default;
    text-decoration: none;
    /* 去掉下划线 */
}

.pagination-button.disabled {
    background-color: #ddd;
    color: #666;
    cursor: not-allowed;
}
.mycourse[data-v-46ac34bc] {
    display: flex;
    background-color: #f9f9f9;
    padding: 5px 10px 20px 10px;
}
</style>